﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace XMLparsingExercise
{
    class XmlParsing
    {
        static Dictionary<string, int> ExtractAuthors(string xmlPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);

            XmlNode rootnode = xmlDoc.DocumentElement;
            Dictionary<string, int> authors = new Dictionary<string, int>();

            foreach (XmlNode node in rootnode.ChildNodes)
            {
                string artist = node["artist"].InnerText;

                if (!authors.Keys.Contains(artist))
                {
                    authors.Add(artist, 1);
                }
                else
                {
                    authors[artist]++;
                }
            }

            return authors;
        }

        static Dictionary<string, int> ExtractAuthorsByXpath(string xmlPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            string xPathQuery = "/albums/album";

            XmlNodeList albums = xmlDoc.SelectNodes(xPathQuery);
            Dictionary<string, int> authors = new Dictionary<string, int>();

            foreach (XmlNode node in albums)
            {
                string artist = node.SelectSingleNode("artist").InnerText;
                if (!authors.Keys.Contains(artist))
                {
                    authors.Add(artist, 0);
                }
                authors[artist]++;
            }

            return authors;
        }

        static void DeleteExpensiveAlbums(string xmlPath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);

            XmlNode rootNode = xmlDoc.DocumentElement;
            for (int i = 0; i < rootNode.ChildNodes.Count; i++)
            {
                string priceStr = rootNode.ChildNodes[i]["price"].InnerText;
                decimal price = decimal.Parse(priceStr);

                if (price >= 20)
                {
                    rootNode.RemoveChild(rootNode.ChildNodes[i]);
                    i--;
                }
                
            }

            xmlDoc.Save("../../CatalogueUpdated.xml");
        }

        static List<string> ExtractSongs(string xmlPath)
        {
            List<string> songs = new List<string>();

            using (XmlReader xmlReader = XmlReader.Create(xmlPath))
            {
                while (xmlReader.Read())
                {
                    if ((xmlReader.NodeType == XmlNodeType.Element) && 
                        xmlReader.Name == "title")
                    {
                        string title = xmlReader.ReadElementString();
                        songs.Add(title);
                    }
                }
            }

            return songs;
        }

        static List<string> ExtractSongsViaLinq(string xmlPath)
        {
            XDocument xmlDoc = XDocument.Load(xmlPath);
            var songs =
                from title in xmlDoc.Descendants("title")
                select title.Value;

            return new List<string>(songs); 
        }

        static void WritePeopleToXml(List<Person> people)
        {
            string fileName = "../../People.xml";
            Encoding encoding = Encoding.UTF8;

            using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("people");

                foreach (Person person in people)
                {
                    person.WritePerson(writer);
                }

                writer.WriteEndDocument();
            }
        }

        static void WriteFromCatalogueToAlbum()
        {
            string catalogue = "../../Catalogue.xml";
            string album = "../../album.xml";

            using (XmlReader reader = XmlReader.Create(catalogue))
            {
                using(XmlTextWriter writer = new XmlTextWriter(album, Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.IndentChar = '\t';
                    writer.Indentation = 1;

                    writer.WriteStartDocument();
                    writer.WriteStartElement("albums");

                    while(reader.Read())
                    {
                        if((reader.NodeType == XmlNodeType.Element) && reader.Name == "album")
                        {
                            reader.Read();
                            string name = reader.ReadElementString();
                            reader.Read();
                            string artist = reader.ReadElementString();
                            writer.WriteStartElement("album");
                            writer.WriteElementString("name", name);
                            writer.WriteElementString("artist", artist);
                            writer.WriteEndElement();
                        }
                    }

                    writer.WriteEndElement();
                }
            }
        }

        static void Main(string[] args)
        {
            string catalogue = "../../Catalogue.xml";
            List<Person> people = Person.GetSampleList();

            WriteFromCatalogueToAlbum();

        }

        private static void PrintSongs(IEnumerable<string> songs)
        {
            foreach (string song in songs)
            {
                Console.WriteLine(song);
            }
        }

        private static void PrintAuthors(Dictionary<string, int> dict)
        {
            foreach (string key in dict.Keys)
            {
                Console.WriteLine("Artist: {0} Albums: {1}", key, dict[key]);
            }
        }
    }
}
